// pages/callList/callList.js
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_title: '呼叫列表',
    userList: [
      // {
      //   headImg: "../images/user-1.jpg",
      //   phoneNum: "18091834069"
      // },
      // {
      //   headImg: "../images/user-2.jpg",
      //   phoneNum: "13458896006"
      // }
    ]
  },

  bindCallUser: function (event) {
    const that = this;
    
    let index = event.currentTarget.dataset.userid;

    wx.setStorage({
      key: 'callUser',
      data: that.data.userList[index],
    });

    // wx.redirectTo({
    //   url: '/pages/calling/calling',
    // });
    wx.redirectTo({
      url: '/pages/addressEdit/addressEdit',
    });
  },

  // 从服务器获取呼叫列表
  getCallList: function () {
    const that = this; 
    const url_callList = app.globalData.baseUrl + '/getPerson';

    wx.showLoading({
      title: '获取呼叫列表中...',
    });

    wx.request({
      url: url_callList,
      method: 'post',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        that.setData({
          userList: res.data
        });
      },
      fail: function (error) {
        console.log('获取呼叫列表失败：', error.errMsg);

        wx.showModal({
          title: '',
          content: '获取列表失败，返回首页',
          showCancel: false,
          confirmText: '确定',
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/index/index',
              })
            }
          }
        })
        
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCallList();
  }

})